package com.erp.module.faHuoDanJu

import android.content.Context
import android.util.Log
import com.erp.bean.FaHuoBean
import com.erp.contacts.Contacts
import com.erp.contacts.Contacts.BOUNDARY
import com.erp.Interface.OnUserDataInterface
import com.erp.module.faHuoDanJu.faHuo1.FragmentFahuo
import com.erp.module.faHuoDanJu.faHuo2.FaHuoCarAvtivity
import com.erp.util.Httputil.*
import org.xutils.http.RequestParams
import org.xutils.x
import java.io.File

/**
 * Created by Administrator on 2018/11/13/013.
 * 作者：刘洋
 * 上传发货单据model
 */
class AddFaHuoModel : FaHuoDanJuContract.FaHuoMP {


    /** 获取单据列表*/
    override fun getdanju(type: Int, context: Context, page: Int, pagesize: Int, zhuanmaidian: String, baohao: String, onUserDataInterface: OnUserDataInterface) {
        var params: RequestParams? = null
        if (type == FragmentFahuo.DANJU) {
            params = RequestParams(Contacts.SHOWDANJU)
        } else if (type == FragmentFahuo.QINGDAN) {
            params = RequestParams(Contacts.SHOWQINGDAN)
        }
        getparams(context, params)
        setProsess(params)
        params!!.addBodyParameter("page", page.toString())
        params.addBodyParameter("pagesize", pagesize.toString())
        if (zhuanmaidian != "") {
            params.addBodyParameter("searchParams[shop_name]", zhuanmaidian)
        }
        if (baohao != "") {
            params.addBodyParameter("searchParams[onum]", baohao)
        }
        x.http().get(params, Call(context, onUserDataInterface))
    }

    /**获取车次号质量列表 */
    override fun getCarShuJu(context: Context, page: Int, pagesize: Int, carnumble: String, iscarnumble: Boolean, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.FAHUO_WULIU)
        getparams(context, params)
        setProsess(params)
        params.addBodyParameter("page", page.toString())
        params.addBodyParameter("pagesize", pagesize.toString())
        if (iscarnumble) {
            params.addBodyParameter("searchParams[iscarnumble]", "1")
            params.addBodyParameter("searchParams[car_name]", carnumble)
        } else {
            params.addBodyParameter("searchParams[iscarnumble]", "2")
            params.addBodyParameter("searchParams[onum]", carnumble)
        }
        Log.d("getCarShuJu", "getCarShuJugetCarShuJugetCarShuJu")
        x.http().get(params, Call(context, onUserDataInterface))
    }


    /**搜索车次号，获取订单号 */
    override fun getdanju(context: Context, car_name: String, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.FAHUO_WULIU + "/" + car_name)
        getparams(context, params)
        setProsess(params)
        params.addHeader("ACCEPT", "application/json")
        x.http().get<String>(params, Call(context, onUserDataInterface))
    }

    /**上传专卖店排序*/
    override fun setcarweight(context: Context, fahuobean: FaHuoBean, onUserDataInterface: OnUserDataInterface) {
        var params = RequestParams(Contacts.FAHUO_WULIU + "/" + fahuobean.car_number + "/" + "edit_upload_weight")
        getparams(context, params)
        addtoken(params)
        setProsess(params)
        params.addBodyParameter("dcn_id", fahuobean.dcn_id, BOUNDARY)
        for (i in fahuobean.shops.indices) {
            var j = i + 1;
            params.addBodyParameter("car[$i][exclusive_shop]", fahuobean.shops[i].exclusive_shop, BOUNDARY)
            params.addBodyParameter("car[$i][weight]", j, BOUNDARY)
        }
        params.addHeader("ACCEPT", "application/json")
        x.http().post(params, Call(context, onUserDataInterface))
    }

    /**上传专卖店订单排序*/
    override fun setshoponumweight(context: Context, fahuobean: FaHuoBean, position: Int, onUserDataInterface: OnUserDataInterface) {
        var params = RequestParams(Contacts.FAHUO_WULIU + "/" + fahuobean.car_number + "/" + "edit_deliver_car_order_weight")
        var shop = fahuobean.shops[position]
        getparams(context, params)
        addtoken(params)
        setProsess(params)
        params.addBodyParameter("dcn_id", fahuobean.dcn_id, BOUNDARY)
        params.addBodyParameter("exclusive_shop", shop.exclusive_shop, BOUNDARY)
        for (i in shop.onums.indices) {
            var j = i + 1;
            params.addBodyParameter("shop[$i][oid]", shop.onums[i].oid, BOUNDARY)
            params.addBodyParameter("shop[$i][weight]", j, BOUNDARY)
        }
        params.addHeader("ACCEPT", "application/json")
        x.http().post(params, Call(context, onUserDataInterface))
    }


    /**上传发货清单和物流单据照片 */
    override fun addFaHuo(context: Context, car_numble: String, shop_name: String, listphoto: List<FaHuoBean.photo>, deletephoto: List<FaHuoBean.photo>, type: Int, onUserDataInterface: OnUserDataInterface) {
        var params = RequestParams()
        when (type) {
            FaHuoCarAvtivity.WuLiu -> {
                params = RequestParams(Contacts.FAHUO_WULIU + "/" + car_numble + "/" + "edit_upload_dv")
            }
            FaHuoCarAvtivity.FaHuo -> {
                params = RequestParams(Contacts.FAHUO_WULIU + "/" + car_numble + "/" + "edit_upload_dl")
            }
        }
        getparams(context, params)
        addtoken(params)
        setProsess(params)
        params.addBodyParameter("", "", BOUNDARY)
        params.addBodyParameter("carname", car_numble, BOUNDARY)
        params.addBodyParameter("shop_name", shop_name, BOUNDARY)
        var j = 0
        for (i in listphoto.indices) {
            if (listphoto[i].fid == "") {
                j++
                params.addBodyParameter("files[new][$j]", File(listphoto[i].url), BOUNDARY)
            }
        }
        for (i in deletephoto.indices) {
            params.addBodyParameter("files[delete][" + deletephoto[i].fid + "]", deletephoto[i].fid, BOUNDARY)
        }
        params.addHeader("ACCEPT", "application/json")
        x.http().post(params, Call(context, onUserDataInterface))
    }
}
